package de.dhbw.asa.model;

import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 * Contains all the interaction with a Customer and its Data
 * @author aar
 *
 */

@ManagedBean
@SessionScoped
public class Customer extends Database
{

	int iCustomerNr;
	String sForename;
	String sLastname;
	String sTitle;
	String sPassword;
	String sEnteredPassword;
	
	boolean bLoggedIn=false;
	
	public List jAdressList;
	
	/*
	 * loads an existing Customer
	 */
	public boolean load(int iCustomerNr)
	{
		boolean bReturn = false;
		String sCustomerNr = String.valueOf(iCustomerNr);
		StringBuffer sbQuery = new StringBuffer();
		sbQuery.append("Select * from Customer where CustomerNr = '");
		sbQuery.append(sCustomerNr);
		sbQuery.append("';");
		try
		{
			if (this.createConnection())
			{
				ResultSet jResult = this.executeQuery(sbQuery.toString());
				if (jResult.next())
				{
					this.iCustomerNr = iCustomerNr;
					this.sForename = jResult.getString("forename");
					this.sLastname = jResult.getString("lastname");
					this.sTitle = jResult.getString("title");
					this.sPassword = jResult.getString("password");
				}
				bReturn = true;
				this.closeConnection();
				this.jAdressList=this.loadAllAdresses();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	/*
	 * prepares a list with all the adresses of a customer
	 */
	public List<Adress> loadAllAdresses()
	{
		List<Adress> jList = new ArrayList<Adress>();
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("Select * from Adress where customerNr =");
			sbQuery.append(this.iCustomerNr);
			sbQuery.append(";");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			while (jResult.next())
			{
				Adress jAdress = new Adress();
				jAdress.load(jResult.getInt("adressNr"));
				jList.add(jAdress);
			}
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return jList;
	}
	
	/*
	 * Saves the changes of a customer
	 */
	public String saveChanges()
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Insert the customer into the Database
			sbQuery.append("update customer set forename='");
			sbQuery.append(this.sForename);
			sbQuery.append("', lastname ='");
			sbQuery.append(this.sLastname);
			sbQuery.append("', title ='");
			sbQuery.append(this.sTitle);
			sbQuery.append("', password ='");
			sbQuery.append(this.sPassword);
			sbQuery.append("' where customerNr =");
			sbQuery.append(this.iCustomerNr);
			sbQuery.append(";");
			this.execute(sbQuery.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	/*
	 * saves a new Customer in the Database
	 */
	public String saveNewCustomer(String sForename, String sLastname, String sTitle, String sPassword)
	{
		String sReturn = "";
		this.sForename = sForename;
		this.sLastname = sLastname;
		this.sTitle = sTitle;
		this.sPassword = sPassword;
		
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Finding out the last CustomerNr of our Customers
			sbQuery.append("Select customerNr from Customer;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			int iLastCustomerNr = 0;
			while (jResult.next())
			{
				iLastCustomerNr = jResult.getInt("CustomerNr");
			}
			this.iCustomerNr = iLastCustomerNr+1;
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the customer into the Database
			sbQuery2.append("insert into customer (customerNr, forename, lastname, title, password) values (");
			sbQuery2.append(this.iCustomerNr);
			sbQuery2.append(",'");
			sbQuery2.append(this.sForename);
			sbQuery2.append("','");
			sbQuery2.append(this.sLastname);
			sbQuery2.append("','");
			sbQuery2.append(this.sTitle);
			sbQuery2.append("','");
			sbQuery2.append(this.sPassword);
			sbQuery2.append("');");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}

	public int getiCustomerNr() 
	{
		return iCustomerNr;
	}

	public void setiCustomerNr(int iCustomerNr) 
	{
		this.iCustomerNr = iCustomerNr;
	}

	public String getsForename() 
	{
		return sForename;
	}

	public void setsForename(String sForename) 
	{
		this.sForename = sForename;
	}

	public String getsLastname() 
	{
		return sLastname;
	}

	public void setsLastname(String sLastname) 
	{
		this.sLastname = sLastname;
	}

	public String getsTitle() 
	{
		return sTitle;
	}

	public void setsTitle(String sTitle) 
	{
		this.sTitle = sTitle;
	}

	public String getsPassword() 
	{
		return sPassword;
	}

	public void setsPassword(String sPassword) 
	{
		this.sPassword = sPassword;
	}

	public String getsEnteredPassword() 
	{
		return sEnteredPassword;
	}

	public void setsEnteredPassword(String sEnteredPassword) 
	{
		this.sEnteredPassword = sEnteredPassword;
	}

	public List getjAdressList() {
		return jAdressList;
	}

	public void setjAdressList(List jAdressList) {
		this.jAdressList = jAdressList;
	}

	public boolean isbLoggedIn() {
		return bLoggedIn;
	}

	public void setbLoggedIn(boolean bLoggedIn) {
		this.bLoggedIn = bLoggedIn;
	}
	
	
	
}
